<template>
    <div>





    </div>
</template>


<script>
import {defineComponent} from 'vue'

export default defineComponent({
    name: "ProductInfo",
    data() {
        return {
            account: {},
            userId: '',
            favoriteId: '',
            favorite: {},
            productId: '',
            product: {},
            store: {},
            storeId: '',
            quantity: 1,            // 购买的数量
            maxQuantity: 10,
            numberStatus: false,
            isFavorite: false,
            activeName: 'first',
            cartId: '',
            cart: {},


        }
    },
    mounted() {
        // 进入页面就加载用户信息
        this.account = JSON.parse(localStorage.getItem('account'))
        this.userId = this.account.id
        this.productId = this.$route.query.id

        // 跳转到该页面时候未携带参数，则返回
        if (this.productId === '' || this.productId === undefined || this.productId === null) {
            this.goBack()
            return
        }
        this.loadProductAndStore()

        // console.log(this.maxQuantity)

    },
    methods: {

        checkFavorite() {
            this.$request.post('/userFavoriteProduct/checkFavoriteByUserIdAndProductId?userId=' + this.userId + '&productId=' + this.productId).then(res => {
                if (res.code === '200') {
                    if (res.data === null || res.data === undefined || res.data === '') {
                        this.isFavorite = false
                    } else {
                        this.favoriteId = res.data.id
                        this.favorite = res.data
                        this.isFavorite = true
                    }
                    // console.log(this.favoriteId)
                    // console.log(this.favorite)
                } else {
                    this.$message.error(res.msg)
                }
            })
        },

        loadStore() {
            this.$request.get('/store/storeInfo?id=' + this.product.storeId).then(res => {
                if (res.code === '200') {
                    this.store = res.data
                    // console.log(this.store)
                }
            })
        },

        loadProductAndStore() {
            this.$request.get('/product/productInfo?id=' + this.productId).then(res => {
                if (res.code === '200') {
                    this.product = res.data
                    this.storeId = this.product.storeId
                    // console.log(this.product)
                    if (this.product.stock <= 0) {
                        console.log('库存不足')
                        this.numberStatus = true
                    } else {
                        this.numberStatus = false
                    }
                    // 优化：购买的最大数量
                    this.maxQuantity = this.product.stock > 10 ? 10 : this.product.stock
                    this.loadStore()
                }
            })
        },

        goBack() {
            this.$router.go(-1)
        },

        handleClick(tab, event) {
            console.log(tab, event);
        }
    }
})
</script>


<style scoped>

</style>